package org.NooLab.itexx.comm.messages;

import java.io.Serializable;
import java.util.ArrayList;

import org.NooLab.itexx.comm.util.XmlStringHandling;
import org.NooLab.utilities.datatypes.ValuePair;
import org.NooLab.utilities.datatypes.ValuePairs;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;




@SuppressWarnings("rawtypes")
public class MessageMap implements Serializable{
 
	private static final long serialVersionUID = -299333312504698447L;
	 
	
	ValuePairs xEntries = new ValuePairs();
	
	BidiMap xContent = new DualHashBidiMap();
	
	transient final String rootStr = "itx";
	
	transient ValuePair vp ;
	transient XmlStringHandling xMsg = new XmlStringHandling();
	
	// ========================================================================	
	public MessageMap(){
		
	}
	// ========================================================================


	public void transportMsg(Object msgObj) {
		
		String cn,str,msgStr;
		
		String[] mstrings; 
		
		cn = (msgObj.getClass().getSimpleName()) ;
		
		if (cn.contentEquals("String[]")){
			mstrings = (String[])msgObj ;
			str = mstrings[1] ; 
		} else { 
			str = (String)msgObj; 
		}
		
		
		if ( str.startsWith("@iTexx :: ") == false){
			return;
		}
		
		str = str.replace("@iTexx ::", "").trim() ;
		
		msgStr =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<itx>\n" + str + "\n</itx>";
		// 
		
		/*  
		 *   <action name="startup" value="completed" code="0" />
		 *   <process name="docoserv" secret="askdfh-876asdf-38-sf87sdsdfja" responseport="" />]
		 */
		
		
		
		xMsg.setContentRoot( rootStr ) ;
		
		registerInfo( msgStr, "action", "name", "action");
		registerInfo( msgStr, "action", "value" );
		registerInfo( msgStr, "action", "code");
		
		registerInfo( msgStr, "process", "name", "process");
		registerInfo( msgStr, "process", "secret");
		registerInfo( msgStr, "process", "responseport");

	}


	
	@SuppressWarnings("unchecked")
	private void registerInfo( String xstr, String tag, String attr) {
		registerInfo( xstr, tag, attr,"") ; 
	}
	
	private void registerInfo( String xstr, String tag, String attr, String mapstr) {
		String str;
		
		ValuePair vp = new ValuePair( tag, attr);// "action", "name");
		str = xMsg.getSpecifiedInfo(xstr, tag, attr);  
		
		if (mapstr.length()==0){
			mapstr = attr;
		}
		xContent.put( mapstr, str);
		
		// xContent.put( vp, str);
		xContent.put( tag+"::"+ attr, str);
		xEntries.add(vp) ;
	}
	
	
	public ValuePairs getEntries() {
		return xEntries;
	}


	public BidiMap getxContent() {
		return xContent;
	}


	// ------------------------------------------------------------------------
	

	public void setEntries( ValuePairs entries) {
		xEntries = entries;
	}


	public void setxContent(BidiMap xContent) {
		this.xContent = xContent;
	}
	
	
}
